package com.sgolub.commanndsonline.dao.entity;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sgolub.commanndsonline.domain.proxy.ProxyUserBasketTotal;

@Repository
public class ProxyUserBasketTotalDAOImpl implements ProxyUserBasketTotalDAO {

    @Autowired
    private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<ProxyUserBasketTotal> listByUserId(Integer userId) {
		System.out.println("==== ProxyUserBasketTotalDAOImpl.listByUserId ====");
		String sql = "from ProxyUserBasketTotal where fk_sec_user_id = " + userId +
				" order by support_type_name";
		Query queryH = sessionFactory.getCurrentSession().createQuery(sql);
		List<ProxyUserBasketTotal> userBasketListTotal = queryH.list();
		
		Iterator<ProxyUserBasketTotal> itrTotal = userBasketListTotal.iterator();
		
		while(itrTotal.hasNext()) {
		ProxyUserBasketTotal elementTotal = itrTotal.next();
		System.out.println("ZZ = " + elementTotal.getSupportTypeName());
   	 	}
		
		
		
		return userBasketListTotal;
	}

	
	
	public Integer totalSumByUserId(Integer userId) {
		Integer resultSum = 0;
		List<ProxyUserBasketTotal> userBasketListTotal = listByUserId(userId);
		Iterator<ProxyUserBasketTotal> itrTotal = userBasketListTotal.iterator();
		
		while(itrTotal.hasNext()) {
		ProxyUserBasketTotal elementTotal = itrTotal.next();
		resultSum = resultSum + elementTotal.getSum();
   	 	}
   	 
		return resultSum;
	}

}
